package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.SysSetupEntity;
import com.ly.heart.domain.dto.SysSetupDto;
import com.ly.heart.mapper.SysSetupMapper;
import com.ly.heart.service.ISysSetupService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统设置 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-10-02
 */
@Service
public class SysSetupServiceImpl extends ServiceImpl<SysSetupMapper, SysSetupEntity> implements ISysSetupService {

    @Override
    public AjaxResult list ( SysSetupEntity sysSetupEntity ) {
        LambdaQueryWrapper<SysSetupEntity> queryWrapper = new LambdaQueryWrapper<> ();
        if ( sysSetupEntity.getBizType () != null ) {
            queryWrapper.eq ( SysSetupEntity :: getBizType, sysSetupEntity.getBizType () );
        }
        List<SysSetupEntity> list = this.list ( queryWrapper );
        return AjaxResult.success ( list );
    }

    @Override
    public SysSetupEntity querySingle ( SysSetupEntity sysSetupEntity ) {
        LambdaQueryWrapper<SysSetupEntity> queryWrapper = new LambdaQueryWrapper<> ();
        if ( sysSetupEntity.getBizType () != null ) {
            queryWrapper.eq ( SysSetupEntity :: getBizType, sysSetupEntity.getBizType () );
        }
        if ( StringUtils.isNotBlank ( sysSetupEntity.getDicKey () ) ) {
            queryWrapper.eq ( SysSetupEntity :: getDicKey, sysSetupEntity.getDicKey () );
        }
        SysSetupEntity setupEntity = this.getOne ( queryWrapper );
        return setupEntity;
    }

    @Override
    public AjaxResult saveSingle ( SysSetupDto sysSetupDto ) {
        sysSetupDto.getSysSetupEntities ().forEach ( sysSetupEntity -> {
            this.remove ( new LambdaUpdateWrapper<SysSetupEntity> ().
                    eq ( SysSetupEntity :: getBizType, sysSetupEntity.getBizType () ).
                    eq ( SysSetupEntity :: getDicKey, sysSetupEntity.getDicKey () ) );
            sysSetupEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
            sysSetupEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
            sysSetupEntity.setCreateTime ( DateUtils.getNowDate () );
            boolean save = this.save ( sysSetupEntity );
            if ( ! save ) {
                throw new CustomException ( "新增客户设置参数失败！" );
            }
        } );
        return AjaxResult.success ();
    }

    @Override
    public AjaxResult updateSingle ( SysSetupEntity sysSetupEntity ) {
        return null;
    }

    @Override
    public AjaxResult deleteSingle ( String id ) {
        return null;
    }
}
